package net.zjitc.myqueue.myservice;

import net.zjitc.myqueue.LinkedQu;
import net.zjitc.myqueue.MyQu;
import net.zjitc.myqueue.exception.MyQuException;

import java.util.Scanner;

/**
 * Created by Administrator on 2017/4/6.
 */
public class MachineQueue extends Thread{
    /*队列由机器产生*/
        private MyQu<Integer> queue ;
        private Integer number = 0;

    /*接收最大容量maxlength，
    然后根据maxtlength来new一个队列出来，交赋值给机器
    * */
    public MachineQueue(Integer maxlength){
        queue = new LinkedQu<>(maxlength);
    }

    @Override
    public void run() {
        /*键盘输入，当按回车时产生号码*/
            Scanner scanner = new Scanner(System.in);
            while (true){
                System.out.println("请按回车取号码");
                scanner.nextLine();/*按回车*/
                /*已经按下回车，怎么处理？？？*/
                number++;
                Integer preNum = number;//号码产生后是要入队的
                try { //捕获异常信息（当队列满时的捕获）
                    queue.add(preNum);
                    System.out.println("你是 "+preNum+"号 ，你前面有"+(queue.size()-1)+"个人在排队");
                } catch (MyQuException e) {
                    number--;
                    System.out.println(e.getMessage());
                }

            }
    }

    public MyQu<Integer> getQueue(){
        return this.queue;
    }
}
